//
//  YWPuzzleController.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/7/25.
//

#import "YWPuzzleController.h"
#import "YWFreedomPuzzleController.h"
#import "YWJointPuzzleController.h"
#import "YWPosterPuzzleController.h"
#import "YWTemplatePuzzleController.h"
#import "YWPuzzleChildController.h"

@interface YWPuzzleController ()

@property(nonatomic,strong)YWFreedomPuzzleController *freedomPuzzleController;
@property(nonatomic,strong)YWJointPuzzleController *jointPuzzleController;
@property(nonatomic,strong)YWPosterPuzzleController *posterPuzzleController;
@property(nonatomic,strong)YWTemplatePuzzleController *templatePuzzleController;

@property(nonatomic,strong)YWPuzzleChildController *showingController;

@property (weak, nonatomic) IBOutlet UIView *bgView;


- (IBAction)dismissClick:(UIButton *)sender;
- (IBAction)conformBtnClick:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UIView *layerView;
- (IBAction)posterBtnClick:(UIButton *)sender;
- (IBAction)templateBtnClick:(UIButton *)sender;
- (IBAction)jointBtnClick:(UIButton *)sender;
- (IBAction)freedomBtnClick:(UIButton *)sender;

@end

@implementation YWPuzzleController

-(YWFreedomPuzzleController *)freedomPuzzleController{
    if(_freedomPuzzleController == nil){
        _freedomPuzzleController = [[YWFreedomPuzzleController alloc] initWithNibName:@"YWFreedomPuzzleController" bundle:nil];
        [self addChildViewController:_freedomPuzzleController];
        _freedomPuzzleController.photos = self.photos;
        _freedomPuzzleController.assets = self.assets;
    }
    return _freedomPuzzleController;
}

-(YWJointPuzzleController *)jointPuzzleController{
    if(_jointPuzzleController == nil){
        _jointPuzzleController = [[YWJointPuzzleController alloc] initWithNibName:@"YWJointPuzzleController" bundle:nil];
        [self addChildViewController:_jointPuzzleController];
        _jointPuzzleController.photos = self.photos;
        _jointPuzzleController.assets = self.assets;
    }
    return _jointPuzzleController;
}

-(YWPosterPuzzleController *)posterPuzzleController{
    if(_posterPuzzleController == nil){
        _posterPuzzleController = [[YWPosterPuzzleController alloc] initWithNibName:@"YWPosterPuzzleController" bundle:nil];
        [self addChildViewController:_posterPuzzleController];
        _posterPuzzleController.photos = self.photos;
        _posterPuzzleController.assets = self.assets;
    }
    return _posterPuzzleController;
}

-(YWTemplatePuzzleController *)templatePuzzleController{
    if(_templatePuzzleController == nil){
        _templatePuzzleController = [[YWTemplatePuzzleController alloc] initWithNibName:@"YWTemplatePuzzleController" bundle:nil];
        [self addChildViewController:_templatePuzzleController];
        _templatePuzzleController.photos = self.photos;
        _templatePuzzleController.assets = self.assets;
    }
    return _templatePuzzleController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.bgView addSubview:self.templatePuzzleController.view];
    [self.bgView addSubview:self.freedomPuzzleController.view];
    [self.bgView addSubview:self.jointPuzzleController.view];
    [self.bgView addSubview:self.posterPuzzleController.view];
    
    self.showingController = self.posterPuzzleController;
    
    MJWeakSelf
    [self.jointPuzzleController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.bgView);
    }];
    [self.posterPuzzleController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.bgView);
    }];
    [self.templatePuzzleController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.bgView);
    }];
    [self.freedomPuzzleController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.bgView);
    }];
    
}


-(void)dismiss{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)dismissClick:(UIButton *)sender{
    [self dismiss];
    
}
- (IBAction)conformBtnClick:(UIButton *)sender{
    
    if([self.delegate respondsToSelector:@selector(puzzleController:newImage:)]){

        [self.delegate puzzleController:self newImage:[self.showingController getCurrentLayer]];

    }
    
    //把图片保存到系统相册当中
    //注意:@selector里面的方法不能够瞎写,必须得是image:didFinishSavingWithError:contextInfo:
    UIImageWriteToSavedPhotosAlbum([self.showingController getCurrentLayer], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
    [self dismiss];
    
    
    
}

//保存完毕时调用
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSLog(@"success");
    
}

- (IBAction)posterBtnClick:(UIButton *)sender {
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.layerView.transform = CGAffineTransformIdentity;
    }];
    
    [self.bgView bringSubviewToFront:self.posterPuzzleController.view];
    
    
    self.showingController = self.posterPuzzleController;
}

- (IBAction)templateBtnClick:(UIButton *)sender {
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.layerView.transform = CGAffineTransformMakeTranslation(sender.width * sender.tag, 0);
    }];
    [self.bgView bringSubviewToFront:self.templatePuzzleController.view];
    
    self.showingController = self.templatePuzzleController;
}

- (IBAction)jointBtnClick:(UIButton *)sender {
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.layerView.transform = CGAffineTransformMakeTranslation(sender.width * sender.tag, 0);
    }];
    [self.bgView bringSubviewToFront:self.jointPuzzleController.view];
    
    self.showingController = self.jointPuzzleController;
}

- (IBAction)freedomBtnClick:(UIButton *)sender {
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.layerView.transform = CGAffineTransformMakeTranslation(sender.width * sender.tag, 0);
    }];
    [self.bgView bringSubviewToFront:self.freedomPuzzleController.view];
    
    self.showingController = self.freedomPuzzleController;
}

//获取当前图层
//-(UIImage *)getCurrentBgViewLayer{
//
//    //1.开启一个位图上下文
//    UIGraphicsBeginImageContextWithOptions(self.bgView.size, NO, 0);
//
//    //2.把画板上的内容渲染到上下文当中
//    CGContextRef ctx =  UIGraphicsGetCurrentContext();
//    [self.bgView.layer renderInContext:ctx];
//
//    //3.从上下文当中取出一张图片
//    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//
//    //4.关闭上下文
//    UIGraphicsEndImageContext();
//
//    return newImage;
//}

@end
